import { defineStore } from 'pinia'

import $axios from '../tools/axios'

type FoemData = {
  sid: number
  trainno: string
  fromnm: string
  tonm: string
  waitfor: number
  running: number
  totime: string
  ltime: string
}

export const useScheduleStore = defineStore('schedule', {
  state: () => {
    return {
      dialogFormVisible: false,
      dialogFormTitle: '1234',
      trainno: 'g2604',
      tableData: [],
      /**
       * @type {  sid: number,  trainno: string  fromnm: string  tonm: string  waitfor: number
       *   running: number,  totime: string,  ltime: string }
       */
      formdata: {
        sid: -1,
        trainno: 'G',
        fromnm: '当前站点',
        tonm: '下一站',
        waitfor: 120,
        running: 60,
        totime: '12:12',
        ltime: '12:14'
      }
    }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    preInfo4Add() {
      this.dialogFormTitle = `为 ${this.trainno} 提供新的列车时刻详情`
      this.formdata.trainno = this.trainno
      this.dialogFormVisible = true
    },

    async loadbyno() {
      this.tableData.length = 0
      console.log(this.trainno)
      const path = '/s/q/byno/' + this.trainno
      const { data: res } = await $axios.get(path)
      if (res.code === 200) {
        this.tableData = res.data
      }

      console.log(res)
    },
    /**
     * 加载数据
     */
    async loadall() {
      const path = '/s/q/all'
      const { data: res } = await $axios.get(path)
      if (res.code === 200) {
        this.tableData = res.data
      }
      console.log(res)
    }
  }
})
